import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_house_manager/controller/build_controller.dart';
// import 'package:flutter_house_manager/utils/toast.dart';
import 'package:get/get.dart';

class RoomList extends StatefulWidget {
  const RoomList({super.key});

  @override
  State<RoomList> createState() => _RoomListState();
}

class _RoomListState extends State<RoomList> {
  // 1. 定义初始数据
  BuildController controller = Get.find<BuildController>();

  List roomList = [];
  List rooms = [];
  // 造数据
  fakeData() {
    // 1.随机生成房间的数据5-10
    int roomCount = Random().nextInt(6) + 5;
    // 2.遍历生成楼栋号和房间号
    for (int i = 0; i < roomCount; i++) {
      // 2.1随机生成楼栋号 1-10
      int building = Random().nextInt(10) + 1;
      // 2.2随机生成房间号
      int room = Random().nextInt(4) + 1;
      // 2.3 组合房间号
      String roomNum = '${building}0$room';
      // 2.4去重
      if (rooms.contains(roomNum)) {
        continue;
      }
      // 5.拼接数据
      rooms.add(roomNum);
      setState(() {
        roomList = rooms;
      });
    }
  }

  @override
  void initState() {
    super.initState();
    fakeData();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<BuildController>(builder: (controller) {
      return Scaffold(
          appBar: AppBar(
            title: const Text('选择房间'),
          ),
          body: ListView.builder(
            itemBuilder: (BuildContext context, int index) {
              return GestureDetector(
                onTap: () {
                  // 把房间号存储到getx中
                  controller.buildInfo['room'] = roomList[index];
                  // 把房间号放入到共享参数
                  controller.updateBuildInfo(controller.buildInfo);
                  // 跳转到添加房屋页面
                  Navigator.pushNamed(context, '/houseform');
                },
                child: Container(
                    color: Colors.white,
                    padding: const EdgeInsets.all(10),
                    child: Row(children: [
                      Expanded(
                          child: Text(controller.buildInfo['point'] +
                              controller.buildInfo['building'] +
                              roomList[index] +
                              "室")),
                      const Row(children: [
                        Icon(Icons.arrow_forward_ios,
                            size: 16, color: Colors.black),
                      ])
                    ])),
              );
            },
            itemCount: roomList.length,
          ));
    });
  }
}
